//
//  UseList.swift
//  learn-ios
//
//  Created by Dong on 2022/4/5.
//

import SwiftUI

struct DemoListView: View {
    var body: some View {
        List {
            ForEach(1 ... 5, id: \.self) { id in
                Text("UserId \(id.description)")
            }
        }
    }
}

struct UseList: View {
    // 多选Set
    @State private var multiSelection = Set<UUID>()
    
    let peoples = ModelData().peoples

    var body: some View {
        ScrollView {
            VStack {
                // 列表
                List {
                    Text("A List Item")
                    Text("A Second List Item")
                    Text("A Third List Item")
                }
                .frame(height: 200)

                // 通过数据集合创建列表
                List(peoples) {
                    Text("\($0.name)")
                }
                .frame(height: 250)

                // 设置行点击色调
                List(peoples) {
                    Text("\($0.name)")
                }
                .frame(height: 250)
                .listItemTint(.yellow)

                // 其他
                HStack {
                    NavigationLink {
                        // 支持列表选择
                        NavigationView {
                            List(peoples, selection: $multiSelection) {
                                Text("\($0.name)")
                            }
                            .navigationTitle("列表选择")
                            .toolbar {
                                EditButton()
                            }
                            .refreshable {
                                multiSelection.removeAll()
                            }
                        }
                        Text("已选择: \(multiSelection.count)")
                    } label: {
                        Text("支持列表选择")
                            .padding()
                    }

                    NavigationLink {
                        // 支持列表选择
                        NavigationView {
                            List(selection: $multiSelection) {
                                ForEach(peoples, id: \.id) { people in
                                    Section(header: Text("People: \(people.name)")) {
                                        ForEach(people.childs ?? Array(repeating: .init(name: "None"), count: 1), id: \.id) { child in
                                            Text("child \(child.name)")
                                        }
                                    }
                                }
                            }
                            .navigationTitle("多维列表")
                        }
                    } label: {
                        Text("支持多维列表")
                    }

                    NavigationLink {
                        List {
                            NavigationLink {
                                DemoListView()
                                    .listStyle(.automatic)
                            } label: {
                                Text("automatic(自动)")
                            }

                            NavigationLink {
                                DemoListView()
                                    .listStyle(.plain)
                            } label: {
                                Text("plain(普通)")
                            }
                            
                            NavigationLink {
                                DemoListView()
                                    .listStyle(.grouped)
                            } label: {
                                Text("grouped(分组)")
                            }
                           
                        }
                        .listStyle(.automatic)

                    } label: {
                        Text("列表样式")
                    }
                }
            }
        }
    }
}

struct UseList_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            UseList()
        }
    }
}
